package com.rahms.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Redis健康检查配置
 */
@Component
public class RedisHealthCheck implements CommandLineRunner {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        try {
            // 测试Redis连接
            redisTemplate.opsForValue().set("health_check", "ok");
            String result = (String) redisTemplate.opsForValue().get("health_check");
            if ("ok".equals(result)) {
                System.out.println("✅ Redis连接成功！");
                redisTemplate.delete("health_check");
            } else {
                System.err.println("❌ Redis连接失败！");
            }
        } catch (Exception e) {
            System.err.println("❌ Redis连接异常: " + e.getMessage());
            System.err.println("请确保Redis服务已启动，配置信息：");
            System.err.println("- Host: localhost");
            System.err.println("- Port: 6379");
            System.err.println("- Database: 0");
        }
    }
}
